package com.cndw.kungfu.model.player.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.cndw.kungfu.config.ArtifactConfig;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.NumberProcesser;

/**
 * 神器的集合
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2012-8-24 上午10:01:36
 */
public class Artifact implements Serializable {

	private static final long serialVersionUID = -307005754206995126L;

	private long uid;

	private int score;

	private Map<Integer, ArtifactSub> subs = new HashMap<Integer, ArtifactSub>(2);

	private Map<Integer, int[]> bags = new HashMap<Integer, int[]>();

	private static final int maxSize = 16;

	private static final int nodeLength = 8;

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public Map<Integer, ArtifactSub> getSubs() {
		return subs;
	}

	public void setSubs(Map<Integer, ArtifactSub> subs) {
		this.subs = subs;
	}

	public Map<Integer, int[]> getBags() {
		return bags;
	}

	public void setBags(Map<Integer, int[]> bags) {
		this.bags = bags;
	}

	public ArtifactSub getSubByInit(int actId, boolean init) {
		if (init && !subs.containsKey(actId)) {
			synchronized (this) {
				if (!subs.containsKey(actId)) {
					subs.put(actId, new ArtifactSub());
				}
			}
		}
		return subs.get(actId);
	}

	public ArtifactSub getSub(int actId) {
		return getSubByInit(actId, false);
	}

	private int bagAdd(int quality, int currentId, int currentAtt) {
		int blank = getBlank();
		if (blank <= 0) {
			LogUtil.getMain().error("no blank quality: " + quality + " currentId: " + currentId + " currentAtt: " + currentAtt);
			return blank;
		}
		int[] node = new int[nodeLength];
		node[0] = quality;
		node[1] = currentId;
		node[2] = currentAtt;
		node[7] = 1;// 属性个数

		bags.put(blank, node);
		return blank;
	}

	public synchronized int bagAdd(int quality) {
		int[] rates = ArtifactConfig.getRate(quality);

		int indexId = NumberProcesser.rateIndex(rates, 10000);
		int[] currentId = ArtifactConfig.getId(indexId);

		int aType = currentId[0];
		int aLevel = currentId[1];

		int[] attribute = ArtifactConfig.getAttri(aType);
		int currentAtt = NumberProcesser.mtRand(attribute[aLevel], attribute[aLevel + 1]);
		return bagAdd(quality, aType, currentAtt);
	}

	public void bagPut(int bagId, int[] node) {
		bags.put(bagId, node);
	}

	private synchronized int getBlank() {
		for (int i = 1; i <= maxSize; i++) {
			if (!bags.containsKey(i)) {
				return i;
			}
		}
		return 0;
	}

	public int[] getNodeByBag(int bagId) {
		return bags.get(bagId);
	}

	public boolean isIdle() {
		return bags.size() < maxSize;
	}

	public boolean isBlank(int bagId) {
		return bagId > 0 && bagId <= maxSize && !bags.containsKey(bagId);
	}

	public int[] removeNode(int bagId) {
		return bags.remove(bagId);
	}

	public int[] getAttris() {
		int[] eAdd = new int[10];
		for (Map.Entry<Integer, ArtifactSub> entry: subs.entrySet()) {
			ArtifactSub e = entry.getValue();
			e.setId(entry.getKey());
			int[] eNode = e.getAttris();
			for (int i = 0; i < eNode.length; i++) {
				eAdd[i] += eNode[i];
			}
		}
		return eAdd;
	}

}
